package com.example.micropoint.point.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.core.entity.PointInfo;
import com.example.common.core.entity.PointRule;
import com.example.common.redis.utils.RedisKeyConstant;
import com.example.common.redis.utils.RedisUtils;
import com.example.micropoint.point.mapper.PointInfoMapper;
import com.example.micropoint.point.mapper.PointRuleMapper;
import com.example.micropoint.point.service.IPointInfoService;
import com.example.micropoint.point.service.IPointRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class PointInfoServiceImpl implements IPointInfoService {

    @Autowired
    private PointInfoMapper pointInfoMapper;

    @Autowired
    private PointRuleMapper pointRuleMapper;

    @Autowired
    private IPointRuleService pointRuleService;

    @Override
    public void add(Integer uid, Integer pointType) {

        // QueryWrapper<PointRule> wrapper = new QueryWrapper<>();
        // wrapper.eq("point_type",pointType);
        // // 根据积分类型,查询积分
        // PointRule pointRule = pointRuleMapper.selectOne(wrapper);

        List<PointRule> ruleList = pointRuleService.ruleList();

        PointRule pointRule = ruleList.
                stream().filter(rule ->
                        rule.getPointType().equals(pointType))
                .findFirst()
                .orElse(null);

        if (pointRule != null) {
            PointInfo pointInfo = new PointInfo();
            pointInfo.setUid(uid);
            pointInfo.setPointType(pointType);
            pointInfo.setPoint(pointRule.getPoint());
            pointInfoMapper.insert(pointInfo);
        }
    }
}
